/******************************************************************************
*
* This file is part of FreeTrack (http://code.google.com/p/qtfreetrack).
* BY Willy Gardiol (willy@gardiol.org)
*
* FreeTrack is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FreeTrack is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar.  If not, see <http://www.gnu.org/licenses/>
*
******************************************************************************/

#include "ftlabel.h"

#define FTL_MIN_HEIGHT 24
#define FTL_PADDING 2

FTLabel::FTLabel(const QString& s, QWidget *parent) :
    QWidget(parent)
{
    setText(s);
    initialize();
}

FTLabel::FTLabel(QWidget *parent) :
    QWidget(parent)
{
    setText("");
    initialize();
}

void FTLabel::initialize()
{
    background = QBrush( QColor( 0,0,0 ), Qt::NoBrush );
    foreground = QPen( QColor("white"), 2 );
    setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
    alignment = Qt::AlignLeft;
    text_flags = Qt::TextSingleLine;
}

void FTLabel::setText(const QString & s )
{
    text = s;
    update();
}

QSize FTLabel::sizeHint() const
{
    QSize size( FTL_MIN_HEIGHT, FTL_MIN_HEIGHT );
    if ( text_flags == Qt::TextSingleLine )
    {
        QFontMetrics fm(font());
        size = fm.size( text_flags, text )+QSize( FTL_PADDING*2,FTL_PADDING );
    }
    return size;
}

QSize FTLabel::minimumSizeHint() const
{
    return sizeHint();
}

void FTLabel::setBackground( const QBrush& b )
{
    background = b;
    update();
}

void FTLabel::setForeground( const QPen& p )
{
    foreground = p;
    update();
}

void FTLabel::paintEvent ( QPaintEvent * event )
{
    QPainter painter(this);
    QRect border = rect().adjusted(0,0,-1,-1);

    painter.fillRect(border, background);

    border = painter.boundingRect( border.adjusted( FTL_PADDING,
                                                    FTL_PADDING>>1,
                                                    -FTL_PADDING,
                                                    -(FTL_PADDING>>1)), alignment | Qt::AlignVCenter | text_flags, text);

    painter.setPen( foreground );
    painter.drawText( border,alignment | Qt::AlignVCenter | text_flags,text );
}

void FTLabel::setWordWrap( bool ww )
{
    text_flags = ww ? Qt::TextWordWrap : Qt::TextSingleLine;
    if ( ww )
        setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding );
    else
        setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
    update();
}

void FTLabel::setAlignment( int a )
{
    alignment = a;
    update();
}
